package com.skyd.core.game.td;

import java.util.concurrent.CopyOnWriteArrayList;

import com.skyd.core.vector.Vector2DF;
import com.skyd.core.vector.VectorRect2DF;

public class TDEffect implements ITDEffect {
	
	/**
	 * 构造函数
	 * @param duration 持续时间，随世界时间递减，当变为0时自动移除，当为-1时表示该附加效果永久有效
	 */
	public TDEffect(int duration){
		setDuration(duration);
	}
	
	private void destroy() {
		getOriginalStatus().removeEffect(this);
		setAttachTargetToDefault();
		setEffectToDefault();
	}
	
	/**
	 * 初始值为null。 功能注释：附加效果
	 */
	private ITDEffect _Effect = null;

	/**
	 * 获取Effect。 功能注释：附加效果
	 * 
	 * @return 当前的ITDEffect类型的值
	 */
	@Override
	public ITDEffect getEffect() {
		return _Effect;
	}

	/**
	 * 设置Effect。 功能注释：附加效果
	 * 
	 * @param value
	 *            要设置的ITDEffect类型值
	 */
	@Override
	public void setEffect(ITDEffect value) {
		if (value == null || _Effect == null) {
			_Effect = value;
			if(value!=null)value.setAttachTarget(this);
		} else
			_Effect.setEffect(value);
	}

	/**
	 * 设置Effect为其默认值(null)。 功能注释：附加效果
	 */
	public void setEffectToDefault() {
		setEffect(null);
	}

	@Override
	public ITDActor getFinalStatus() {
		ITDEffect e = getEffect();
		if (e != null)
			return e.getFinalStatus();
		return this;
	}

	@Override
	public void addOnAttachEffectListener(ITDActorAttachEffectListener listener) {
		getAttachTarget().addOnAttachEffectListener(listener);
	}
	
	@Override
	public void addOnRemoveEffectListener(ITDActorRemoveEffectListener listener) {
		getAttachTarget().addOnRemoveEffectListener(listener);
	}

	@Override
	public int getLevel() {
		return getAttachTarget().getLevel();
	}

	@Override
	public float getRadius() {
		return getAttachTarget().getRadius();
	}

	@Override
	public int getHP() {
		return getAttachTarget().getHP();
	}

	@Override
	public int getMaxHP() {
		return getAttachTarget().getMaxHP();
	}

	@Override
	public float getATK() {
		return getAttachTarget().getATK();
	}

	@Override
	public float getSpeed() {
		return getAttachTarget().getSpeed();
	}

	@Override
	public float getAttackDelay() {
		return getAttachTarget().getAttackDelay();
	}

	@Override
	public float getMoveDelay() {
		return getAttachTarget().getMoveDelay();
	}

	@Override
	public float getDirection() {
		return getAttachTarget().getDirection();
	}

	@Override
	public float getRange() {
		return getAttachTarget().getRange();
	}

	@Override
	public void setLevel(int value) {
		getAttachTarget().setLevel(value);
	}

	@Override
	public void setRadius(float value) {
		getAttachTarget().setRadius(value);
	}

	@Override
	public void setHP(int value) {
		getAttachTarget().setHP(value);
	}

	@Override
	public void setMaxHP(int value) {
		getAttachTarget().setMaxHP(value);
	}

	@Override
	public void setATK(float value) {
		getAttachTarget().setATK(value);
	}

	@Override
	public void setSpeed(float value) {
		getAttachTarget().setSpeed(value);
	}

	@Override
	public void setAttackDelay(float value) {
		getAttachTarget().setAttackDelay(value);
	}

	@Override
	public void setMoveDelay(float value) {
		getAttachTarget().setMoveDelay(value);
	}

	@Override
	public void setDirection(float value) {
		getAttachTarget().setDirection(value);
	}

	@Override
	public void setRange(float value) {
		getAttachTarget().setRange(value);
	}

	@Override
	public int getPrice() {
		return getAttachTarget().getPrice();
	}

	@Override
	public void setPrice(int value) {
		getAttachTarget().setPrice(value);
	}

	@Override
	public boolean getIsAirUnits() {
		return getAttachTarget().getIsAirUnits();
	}

	@Override
	public void setIsAirUnits(boolean value) {
		getAttachTarget().setIsAirUnits(value);
	}

	@Override
	public boolean getIsCanAttackAirUnits() {
		return getAttachTarget().getIsCanAttackAirUnits();
	}

	@Override
	public void setIsCanAttackAirUnits(boolean value) {
		getAttachTarget().setIsCanAttackAirUnits(value);
	}

	@Override
	public boolean getIsCanAttackGroundUnits() {
		return getAttachTarget().getIsCanAttackGroundUnits();
	}

	@Override
	public void setIsCanAttackGroundUnits(boolean value) {
		getAttachTarget().setIsCanAttackGroundUnits(value);
	}

	@Override
	public CopyOnWriteArrayList<ITDActor> getActorsInRange() {
		return getAttachTarget().getActorsInRange();
	}

	@Override
	public CopyOnWriteArrayList<ITDTower> getTowersInRange() {
		return getAttachTarget().getTowersInRange();
	}

	@Override
	public CopyOnWriteArrayList<ITDBiont> getBiontInRange() {
		return getAttachTarget().getBiontInRange();
	}

	@Override
	public ITDMapPiece getPlaceMapPiece() {
		return getAttachTarget().getPlaceMapPiece();
	}

	@Override
	public void addOnAttackListener(ITDActorAttackListener listener) {
		getAttachTarget().addOnAttackListener(listener);
	}

	@Override
	public void addOnUnderAttackListener(ITDActorUnderAttackListener listener) {
		getAttachTarget().addOnUnderAttackListener(listener);
	}

	@Override
	public void addOnDieListener(ITDActorDieListener listener) {
		getAttachTarget().addOnDieListener(listener);
	}

	@Override
	public void addOnMoveListener(ITDActorMoveListener listener) {
		getAttachTarget().addOnMoveListener(listener);
	}

	@Override
	public void addOnStandListener(ITDActorStandListener listener) {
		getAttachTarget().addOnStandListener(listener);
	}

	@Override
	public float getX() {
		return getAttachTarget().getX();
	}

	@Override
	public float getY() {
		return getAttachTarget().getY();
	}

	@Override
	public Vector2DF getPosition() {
		return getAttachTarget().getPosition();
	}

	@Override
	public void setPosition(Vector2DF value) {
		getAttachTarget().setPosition(value);
	}

	@Override
	public VectorRect2DF getAreaRect() {
		return getAttachTarget().getAreaRect();
	}

	@Override
	public Vector2DF getSize() {
		return getAttachTarget().getSize();
	}

	@Override
	public float getWidth() {
		return getAttachTarget().getWidth();
	}

	@Override
	public float getHeight() {
		return getAttachTarget().getHeight();
	}

	@Override
	public Vector2DF getCenterPosion() {
		return getAttachTarget().getCenterPosion();
	}

	@Override
	public void update() {
		if(getEffect()!=null)getEffect().update();
		int v = getDuration();
		if (v == 0)
			destroy();
		setDuration(v-1);
	}

	/**
	 * 初始值为-1。 功能注释：持续时间，随世界时间递减，当变为0时自动移除，当为-1时表示该附加效果永久有效
	 */
	private int _Duration = -1;

	/**
	 * 获取Duration。 功能注释：持续时间，随世界时间递减，当变为0时自动移除，当为-1时表示该附加效果永久有效
	 * 
	 * @return 当前的int类型的值
	 */
	@Override
	public int getDuration() {
		return _Duration;
	}

	/**
	 * 设置Duration。 功能注释：持续时间，随世界时间递减，当变为0时自动移除，当为-1时表示该附加效果永久有效
	 * 
	 * @param value
	 *            要设置的int类型值
	 */
	@Override
	public void setDuration(int value) {
		_Duration = value;
	}

	/**
	 * 设置Duration为其默认值(-1)。 功能注释：持续时间，随世界时间递减，当变为0时自动移除，当为-1时表示该附加效果永久有效
	 */
	public void setDurationToDefault() {
		setDuration(-1);
	}

	/**
	 * 初始值为null。 功能注释：附加到的目标
	 */
	private ITDActor _AttachTarget = null;

	/**
	 * 获取AttachTarget。 功能注释：附加到的目标
	 * 
	 * @return 当前的ITDActor类型的值
	 */
	@Override
	public ITDActor getAttachTarget() {
		return _AttachTarget;
	}

	/**
	 * 设置AttachTarget。 功能注释：附加到的目标
	 * 
	 * @param value
	 *            要设置的ITDActor类型值
	 */
	@Override
	public void setAttachTarget(ITDActor value) {
		_AttachTarget = value;
	}

	/**
	 * 设置AttachTarget为其默认值(null)。 功能注释：附加到的目标
	 */
	public void setAttachTargetToDefault() {
		setAttachTarget(null);
	}

	@Override
	public void removeEffect(ITDEffect value) {
		getOriginalStatus().removeEffect(value);
	}

	@Override
	public ITDActor getOriginalStatus() {
		ITDActor t = getAttachTarget();
		if(t instanceof ITDEffect)return ((ITDEffect)t).getOriginalStatus();
		return t;
	}

	@Override
	public boolean getIsDead() {
		return getAttachTarget().getIsDead();
	}

	@Override
	public void setIsDead(boolean value) {
		getAttachTarget().setIsDead(value);
	}
}
